package com.zhou.jianzhi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhou.jianzhi.entity.base.ActiveUser;
import com.zhou.jianzhi.entity.base.PageResult;
import com.zhou.jianzhi.entity.dto.EvaluationDTO;
import com.zhou.jianzhi.entity.po.*;
import com.zhou.jianzhi.entity.vo.EvaluationVO;
import com.zhou.jianzhi.mapper.EvaluationMapper;
import com.zhou.jianzhi.mapper.HrInfoMapper;
import com.zhou.jianzhi.mapper.JobSeekerMapper;
import com.zhou.jianzhi.mapper.RecruitUnitMapper;
import com.zhou.jianzhi.service.EvaluationService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @author 黄嘉杰
* @description 针对表【evaluation(评价表)】的数据库操作Service实现
* @createDate 2025-01-23 14:34:19
*/
@Service
public class EvaluationServiceImpl extends ServiceImpl<EvaluationMapper, Evaluation>
implements EvaluationService {
    @Autowired
    private HrInfoMapper hrInfoMapper;

    @Autowired
    private EvaluationMapper evaluationMapper;

    @Autowired
    private RecruitUnitMapper recruitUnitMapper;
    @Autowired
    private JobSeekerMapper jobSeekerMapper;
    @Override
    public List<EvaluationVO> getList(String type, Integer page, Integer size) {
        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        Long id = activeUser.getUser().getId();
        QueryWrapper<HrInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", id);
        HrInfo hrId = hrInfoMapper.selectOne(queryWrapper);
        if (hrId == null){
            QueryWrapper<JobSeeker> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("user_id", id);
            JobSeeker jobSeeker = jobSeekerMapper.selectOne(queryWrapper1);
            Page<Evaluation> postPage = new Page<>();
            List<EvaluationVO> evaluationVOList = new ArrayList<>();
            if (type.equals("1")) {
                // 1 获取用户给商家评价的评价列表
                QueryWrapper<Evaluation> queryWrapper2 = new QueryWrapper<>();
                queryWrapper2.eq("from_user_id", jobSeeker.getId());
                queryWrapper2.eq("status", 1);
                queryWrapper2.eq("type", 1);
                Page<Evaluation> pageResult = new Page<>();
                postPage = evaluationMapper.selectPage(pageResult, queryWrapper2);
                if (postPage.getRecords().size() > 0) {
                    postPage.getRecords().forEach(evaluation -> {
                    EvaluationVO evaluationVO = new EvaluationVO();
                    evaluationVO.setId(evaluation.getId());
                    evaluationVO.setFromUserId(evaluation.getFromUserId());
                    evaluationVO.setToUserId(evaluation.getToUserId());
                    evaluationVO.setType(evaluation.getType());
                    evaluationVO.setScore(evaluation.getScore());
                    evaluationVO.setContent(evaluation.getContent());
                    evaluationVO.setCreateTime(evaluation.getCreateTime());
                    RecruitUnit recruitUnit = recruitUnitMapper.selectOne(new QueryWrapper<RecruitUnit>().eq("id", evaluation.getToUserId()));
                    evaluationVO.setUserName(recruitUnit.getName());
                    evaluationVOList.add(evaluationVO);

                });}
            }else {
                QueryWrapper<Evaluation> queryWrapper2 = new QueryWrapper<>();
                queryWrapper2.eq("to_user_id", jobSeeker.getId());
                queryWrapper2.eq("status", 1);
                queryWrapper2.eq("type", 0);
                Page<Evaluation> pageResult = new Page<>();
                postPage = evaluationMapper.selectPage(pageResult, queryWrapper2);
                if (postPage.getRecords().size() > 0) {
                postPage.getRecords().forEach(evaluation -> {
                    EvaluationVO evaluationVO = new EvaluationVO();
                    evaluationVO.setId(evaluation.getId());
                    evaluationVO.setFromUserId(evaluation.getFromUserId());
                    evaluationVO.setToUserId(evaluation.getToUserId());
                    evaluationVO.setType(evaluation.getType());
                    evaluationVO.setScore(evaluation.getScore());
                    evaluationVO.setContent(evaluation.getContent());
                    evaluationVO.setCreateTime(evaluation.getCreateTime());
                    RecruitUnit recruitUnit = recruitUnitMapper.selectOne(new QueryWrapper<RecruitUnit>().eq("id", evaluation.getFromUserId()));
                    evaluationVO.setUserName(recruitUnit.getName());
                    evaluationVOList.add(evaluationVO);

                });}
            }
            return evaluationVOList;
        }else {
        Integer recruitUnitId = hrId.getRecruitUnitId();
        Page<Evaluation> postPage = new Page<>();
        List<EvaluationVO> evaluationVOList = new ArrayList<>();
        if (type.equals("1")) {
            // 1 获取用户给商家评价的评价列表
            QueryWrapper<Evaluation> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("to_user_id", recruitUnitId);
            queryWrapper1.eq("status", 1);
            queryWrapper1.eq("type", 1);
            Page<Evaluation> pageResult = new Page<>();
             postPage = evaluationMapper.selectPage(pageResult, queryWrapper1);
            if (postPage.getRecords().size() > 0) {
                postPage.getRecords().forEach(evaluation -> {
                EvaluationVO evaluationVO = new EvaluationVO();
                evaluationVO.setId(evaluation.getId());
                evaluationVO.setFromUserId(evaluation.getFromUserId());
                evaluationVO.setToUserId(evaluation.getToUserId());
                evaluationVO.setType(evaluation.getType());
                evaluationVO.setScore(evaluation.getScore());
                evaluationVO.setContent(evaluation.getContent());
                evaluationVO.setCreateTime(evaluation.getCreateTime());
                JobSeeker jobSeeker = jobSeekerMapper.selectOne(new QueryWrapper<JobSeeker>().eq("id", evaluation.getFromUserId()));
                evaluationVO.setUserName(jobSeeker.getName());
                evaluationVOList.add(evaluationVO);
            });}
        }else {
            QueryWrapper<Evaluation> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("from_user_id", recruitUnitId);
            queryWrapper1.eq("status", 1);
            queryWrapper1.eq("type", 0);
            Page<Evaluation> pageResult = new Page<>();
             postPage = evaluationMapper.selectPage(pageResult, queryWrapper1);

            if (postPage.getRecords().size() > 0) {
                postPage.getRecords().forEach(evaluation -> {
                 EvaluationVO evaluationVO = new EvaluationVO();
                    evaluationVO.setId(evaluation.getId());
                    evaluationVO.setFromUserId(evaluation.getFromUserId());
                    evaluationVO.setToUserId(evaluation.getToUserId());
                    evaluationVO.setType(evaluation.getType());
                    evaluationVO.setScore(evaluation.getScore());
                    evaluationVO.setContent(evaluation.getContent());
                    evaluationVO.setCreateTime(evaluation.getCreateTime());
                 JobSeeker jobSeeker = jobSeekerMapper.selectOne(new QueryWrapper<JobSeeker>().eq("id", evaluation.getToUserId()));
                 evaluationVO.setUserName(jobSeeker.getName());
                    evaluationVOList.add(evaluationVO);

             });}
        }
        return evaluationVOList;}
    }


    @Override
    public void add(Evaluation evaluation) {
        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        Long id = activeUser.getUser().getId();
        QueryWrapper<HrInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", id);
        HrInfo hrId = hrInfoMapper.selectOne(queryWrapper);
        if (hrId != null) {
            Integer recruitUnitId = hrId.getRecruitUnitId();
            evaluation.setFromUserId(Long.valueOf(recruitUnitId));
            if (evaluation.getToUserId() == null){
                throw new RuntimeException("用户id不能为空");
            }
            if (evaluation.getScore() == null){
                throw new RuntimeException("评分不能为空");
            }
            if (evaluation.getContent() == null){
                throw new RuntimeException("评价内容不能为空");
            }
            evaluationMapper.insert(evaluation);
            Long toUserId = evaluation.getToUserId();
            JobSeeker jobSeeker = jobSeekerMapper.selectOne(new QueryWrapper<JobSeeker>().eq("id", toUserId));
            jobSeeker.setIsType(1);
            jobSeekerMapper.updateById(jobSeeker);
        }else {
            QueryWrapper<JobSeeker> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("user_id", id);
            JobSeeker jobSeeker = jobSeekerMapper.selectOne(queryWrapper1);
            evaluation.setFromUserId(Long.valueOf(jobSeeker.getId()));
            evaluationMapper.insert(evaluation);

        }
    }

    @Override
    public void edit(EvaluationDTO evaluation) {
        Evaluation evaluation1 = new Evaluation();
        evaluation1.setUpdateTime(new Date());
        evaluation1.setId(evaluation.getId());
        evaluation1.setContent(evaluation.getContent());
        evaluation1.setScore(evaluation.getScore());
        evaluationMapper.updateById(evaluation1);
    }

    @Override
    public void delete(Long id) {
        Evaluation evaluation = evaluationMapper.selectById(id);
        evaluation.setStatus(0);
        evaluationMapper.updateById(evaluation);
        JobSeeker jobSeeker = jobSeekerMapper.selectOne(new QueryWrapper<JobSeeker>().eq("id", evaluation.getToUserId()));
        jobSeeker.setIsType(0);
        jobSeekerMapper.updateById(jobSeeker);
    }

    @Override
    public Evaluation getDetail(Long id) {
        return null;
    }
}
